﻿namespace SLClient.CustomerControls
{
    using SLClient.Extension;
    using SLClient.Infrastructure;
    using SLClient.ServiceAPI;
    using System;
    using System.Collections.ObjectModel;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Text.RegularExpressions;
    using System.Windows;
    using System.Windows.Input;

    public class LowerInfoViewModel : ViewModelBase
    {
        private Visibility _commitingVisibility;
        private string _confirmPwd;
        private decimal _currentUserFianDian;
        private string _errMsg;
        private decimal _fiandianSpan;
        private bool _isAgent;
        private bool _isMember;
        private readonly LowerInfoWindow _lowerInfoWindow;
        private ObservableCollection<UserInfo> _lowlist;
        private decimal _maxSysFianDian;
        private decimal _minPersonalFianDian;
        private UserInfo _newUserInfo;
        private string _operation;
        private decimal fenjiedian = (ViewDispatcher.CurrentConfig.MaxSiteFiandian - 0.005M);
        private bool isNew;

        public LowerInfoViewModel(LowerInfoWindow lowerInfoWindow, ObservableCollection<UserInfo> lowerlist, UserInfo newUserInfo, string operation)
        {
            this._lowlist = lowerlist;
            this._commitingVisibility = 1;
            this._newUserInfo = newUserInfo;
            this._lowerInfoWindow = lowerInfoWindow;
            this._operation = operation;
            this.CommandInit();
            if (this._operation == "add")
            {
                this._isAgent = true;
                this._isMember = false;
                this.IsEnable = true;
                this.isNew = true;
                this._currentUserFianDian = this._newUserInfo.Fandian * 100M;
                this._fiandianSpan = ViewDispatcher.CurrentConfig.LowerMemberFiandianSpance * 100M;
                this._minPersonalFianDian = 0.00M;
                this.CanVisibility = 0;
                this._maxSysFianDian = Math.Min(ViewDispatcher.CurrentConfig.MaxNewUserFiandian, Math.Max((decimal) 0.0M, (decimal) (ViewDispatcher.LoginUser.Fandian - ViewDispatcher.CurrentConfig.LowerMemberFiandianSpance)));
            }
            else
            {
                decimal fandian;
                this.isNew = false;
                if (this._newUserInfo.UserType == 0)
                {
                    this._isAgent = true;
                    this._isMember = false;
                }
                else
                {
                    this._isAgent = false;
                    this._isMember = true;
                }
                this.IsEnable = false;
                this.CanVisibility = 1;
                this._currentUserFianDian = this._newUserInfo.Fandian * 100M;
                this._minPersonalFianDian = 0.00M;
                this._maxSysFianDian = this._newUserInfo.MaxFiandian;
                if (this._newUserInfo.Fandian >= this.fenjiedian)
                {
                    this._fiandianSpan = ViewDispatcher.CurrentConfig.UpperMemberFiandianSpance * 100M;
                }
                else
                {
                    this._fiandianSpan = ViewDispatcher.CurrentConfig.LowerMemberFiandianSpance * 100M;
                }
                if (!ViewDispatcher.LoginUser.ParentId.HasValue)
                {
                    fandian = ViewDispatcher.LoginUser.Fandian;
                }
                else
                {
                    fandian = (ViewDispatcher.LoginUser.Fandian > this.fenjiedian) ? (ViewDispatcher.LoginUser.Fandian - ViewDispatcher.CurrentConfig.UpperMemberFiandianSpance) : (ViewDispatcher.LoginUser.Fandian - ViewDispatcher.CurrentConfig.LowerMemberFiandianSpance);
                }
                decimal num2 = Math.Min(fandian, this._newUserInfo.MaxFiandian);
                this._maxSysFianDian = Math.Max(this._newUserInfo.Fandian, num2);
            }
        }

        public void CommandInit()
        {
            RelayCommand command2 = new RelayCommand(new Action(this.Save));
            command2.set_IsEnabled(true);
            RelayCommand command = command2;
            this.SaveCommand = command;
        }

        public void Save()
        {
            ServiceClient instance;
            EventHandler<AddNewUserInfoCompletedEventArgs> handler = null;
            EventHandler<UpdateUserInfoCompletedEventArgs> handler2 = null;
            EventHandler<AddNewUserInfoCompletedEventArgs> handler3 = null;
            EventHandler<UpdateUserInfoCompletedEventArgs> handler4 = null;
            this.ErrMsg = "";
            if (this._operation == "add")
            {
                if (string.IsNullOrEmpty(this.NewUserInfo.UserAccount))
                {
                    this.ErrMsg = "账号不能为空";
                }
                else
                {
                    Regex regex = new Regex("^[0-9a-zA-Z]{0,16}$");
                    if (this.NewUserInfo.UserAccount.Length >= 0x10)
                    {
                        this.ErrMsg = "账号太长(小于16位)";
                    }
                    else if (this.NewUserInfo.UserNickName.Length >= 20)
                    {
                        this.ErrMsg = "昵称太长（小于20位)";
                    }
                    else if (this.NewUserInfo.UserPwd.Length < 6)
                    {
                        this.ErrMsg = "密码太短（大于6位）";
                    }
                    else if (this.NewUserInfo.UserPwd != this.ConfirmPwd)
                    {
                        this.ErrMsg = "密码输入不一致";
                    }
                    else if (ViewDispatcher.LoginUser.UserType == 1)
                    {
                        this.ErrMsg = "只有代理能添加会员";
                    }
                    else
                    {
                        this.NewUserInfo.UserType = new int?(this.IsAgent ? 0 : 1);
                        this.NewUserInfo.UserId = Guid.NewGuid();
                        this.NewUserInfo.Fandian = this.CurrentUserFianDian / 100M;
                        this.NewUserInfo.Status = true;
                        this.CommitingVisibility = 0;
                        if (ViewDispatcher.LoginUser.Fandian > ViewDispatcher.CurrentConfig.MaxNewUserFiandian)
                        {
                            this.NewUserInfo.MaxFiandian = ViewDispatcher.CurrentConfig.MaxNewUserFiandian;
                        }
                        else
                        {
                            this.NewUserInfo.MaxFiandian = ((ViewDispatcher.LoginUser.Fandian - ViewDispatcher.CurrentConfig.LowerMemberFiandianSpance) > ViewDispatcher.CurrentConfig.MaxNewUserFiandian) ? ViewDispatcher.CurrentConfig.MaxNewUserFiandian : (ViewDispatcher.LoginUser.Fandian - ViewDispatcher.CurrentConfig.LowerMemberFiandianSpance);
                        }
                        instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
                        if (handler == null)
                        {
                            if (handler3 == null)
                            {
                                handler3 = delegate (object obj, AddNewUserInfoCompletedEventArgs arg) {
                                    if ((arg.get_Error() != null) || (arg.Result == 3))
                                    {
                                        this.ErrMsg = "添加过程发生错误，请联系客服！";
                                    }
                                    else
                                    {
                                        if (arg.Result == 1)
                                        {
                                            this.ErrMsg = "已存在相同用户名";
                                        }
                                        if (arg.Result == 2)
                                        {
                                            this.CommitingVisibility = 1;
                                            this._lowerInfoWindow.Close();
                                            this._lowlist.Add(this.NewUserInfo);
                                        }
                                    }
                                };
                            }
                            handler = handler3;
                        }
                        instance.AddNewUserInfoCompleted += handler;
                        instance.AddNewUserInfoAsync(this.NewUserInfo);
                    }
                }
            }
            else if (this.NewUserInfo.Fandian == (this.CurrentUserFianDian / 100M))
            {
                this.ErrMsg = "未修改任何内容！";
            }
            else
            {
                this.NewUserInfo.Fandian = this.CurrentUserFianDian / 100M;
                instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
                if (handler2 == null)
                {
                    if (handler4 == null)
                    {
                        handler4 = delegate (object obj, UpdateUserInfoCompletedEventArgs arg) {
                            if (arg.get_Error() == null)
                            {
                                this.CommitingVisibility = 1;
                                this._lowerInfoWindow.Close();
                            }
                            else
                            {
                                this.ErrMsg = "更新过程发生错误，请联系客服！";
                            }
                        };
                    }
                    handler2 = handler4;
                }
                instance.UpdateUserInfoCompleted += handler2;
                instance.UpdateUserInfoAsync(this._newUserInfo, false);
            }
        }

        public Visibility CanVisibility { get; set; }

        public Visibility CommitingVisibility
        {
            get
            {
                return this._commitingVisibility;
            }
            set
            {
                this._commitingVisibility = value;
                base.Notify<Visibility>(Expression.Lambda<Func<Visibility>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_CommitingVisibility)), new ParameterExpression[0]));
            }
        }

        public string ConfirmPwd
        {
            get
            {
                return this._confirmPwd;
            }
            set
            {
                this._confirmPwd = value;
                base.Notify<string>(Expression.Lambda<Func<string>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_ConfirmPwd)), new ParameterExpression[0]));
            }
        }

        public decimal CurrentUserFianDian
        {
            get
            {
                return this._currentUserFianDian;
            }
            set
            {
                if (!this.isNew)
                {
                    if ((value > (this.fenjiedian * 100M)) && (this._currentUserFianDian == (this.fenjiedian * 100M)))
                    {
                        this._fiandianSpan = ViewDispatcher.CurrentConfig.UpperMemberFiandianSpance * 100M;
                        value = (this.fenjiedian * 100M) + (ViewDispatcher.CurrentConfig.UpperMemberFiandianSpance * 100M);
                    }
                    else if ((this._currentUserFianDian == (this.fenjiedian * 100M)) && (value <= (this.fenjiedian * 100M)))
                    {
                        this._fiandianSpan = ViewDispatcher.CurrentConfig.LowerMemberFiandianSpance * 100M;
                        value = (this.fenjiedian * 100M) - (ViewDispatcher.CurrentConfig.LowerMemberFiandianSpance * 100M);
                    }
                    this._currentUserFianDian = value;
                }
                else
                {
                    this._currentUserFianDian = value;
                }
                base.Notify<decimal>(Expression.Lambda<Func<decimal>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_FiandianSpan)), new ParameterExpression[0]));
                base.Notify<decimal>(Expression.Lambda<Func<decimal>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_CurrentUserFianDian)), new ParameterExpression[0]));
            }
        }

        public string ErrMsg
        {
            get
            {
                return this._errMsg;
            }
            set
            {
                this._errMsg = value;
                base.Notify<string>(Expression.Lambda<Func<string>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_ErrMsg)), new ParameterExpression[0]));
            }
        }

        public decimal FiandianSpan
        {
            get
            {
                return this._fiandianSpan;
            }
            set
            {
                this._fiandianSpan = value;
                base.Notify<decimal>(Expression.Lambda<Func<decimal>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_FiandianSpan)), new ParameterExpression[0]));
            }
        }

        public bool IsAgent
        {
            get
            {
                return this._isAgent;
            }
            set
            {
                this._isAgent = value;
                base.Notify<bool>(Expression.Lambda<Func<bool>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_IsAgent)), new ParameterExpression[0]));
            }
        }

        public bool IsEnable { get; set; }

        public bool IsMember
        {
            get
            {
                return this._isMember;
            }
            set
            {
                this._isMember = value;
                base.Notify<bool>(Expression.Lambda<Func<bool>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_IsMember)), new ParameterExpression[0]));
            }
        }

        public UserInfo NewUserInfo
        {
            get
            {
                return this._newUserInfo;
            }
            set
            {
                this._newUserInfo = value;
                base.Notify<UserInfo>(Expression.Lambda<Func<UserInfo>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_NewUserInfo)), new ParameterExpression[0]));
            }
        }

        public decimal PersonalMaxFiandian
        {
            get
            {
                return (this._maxSysFianDian * 100M);
            }
        }

        public decimal PersonalMinFianDian
        {
            get
            {
                return this._minPersonalFianDian;
            }
            set
            {
                this._minPersonalFianDian = value;
                base.Notify<decimal>(Expression.Lambda<Func<decimal>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_PersonalMinFianDian)), new ParameterExpression[0]));
            }
        }

        public string Remark
        {
            get
            {
                return (string.Format("{0:0.00%}", this._minPersonalFianDian / 100M) + "-" + string.Format("{0:0.00%}", this._maxSysFianDian));
            }
        }

        public ICommand SaveCommand { get; set; }

        public decimal SysMaxFianDian
        {
            get
            {
                return this._maxSysFianDian;
            }
            set
            {
                this._maxSysFianDian = value;
                base.Notify<decimal>(Expression.Lambda<Func<decimal>>(Expression.Property(Expression.Constant(this, typeof(LowerInfoViewModel)), (MethodInfo) methodof(LowerInfoViewModel.get_SysMaxFianDian)), new ParameterExpression[0]));
            }
        }
    }
}

